#include <iostream>
#include <cstdio>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <iterator>
#include <random>
#include <chrono>
using std::cin;
using std::cout;

#define F(i, l, r) for (auto i = (l); i <= (r); ++i)
#define dF(i, l, r) for (auto i = (l); i >= (r); --i)
#define F2(i, l, r) for (auto i = (l); i < (r); ++i)

void Solve();
int main() {
#ifndef PKRBT
    [[maybe_unused]] auto tmp1_ = std::freopen("color.in", "r", stdin);
    [[maybe_unused]] auto tmp2_ = std::freopen("color.out", "w", stdout);
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int tests = 1;
    cin >> tests;
    F(t, 1, tests)
        Solve();
    return 0;
}

using LL = long long;

template<typename T>
using Vec = std::vector<T>;

constexpr LL InfLL = 0x3f3f3f3f3f3f3f3f;

std::mt19937_64 rng{(unsigned long long)std::chrono::steady_clock().now().time_since_epoch().count()};

const int MN = 200005;
const int MV = 1000005;

int n;
int a[MN], b[MV];
LL s[MN], f[MN][2];

void Solve() {
    cin >> n;
    F(i, 1, n)
        cin >> a[i];
    s[1] = 0;
    F(i, 2, n)
        s[i] = s[i - 1] + (a[i] == a[i - 1] ? a[i] : 0);
    f[1][0] = 0;
    f[1][1] = -InfLL;
    b[a[1]] = 1;
    F(i, 2, n) {
        f[i][0] = std::max(f[i - 1][0], f[i - 1][1]);
        f[i][1] = -InfLL;
        if (b[a[i]]) {
            f[i][1] = std::max(f[b[a[i]]][0], f[b[a[i]]][1]) + a[i] + (s[i - 1] - s[b[a[i]]]);
            if (b[a[i]] != i - 1)
                f[i][1] = std::max(f[i][1], f[b[a[i]] + 1][1] + a[i] + (s[i - 1] - s[b[a[i]]]));
        }
        b[a[i]] = i;
    }
    F(i, 1, n)
        b[a[i]] = 0;
    cout << std::max(f[n][0], f[n][1]) << '\n';
}

/*
TODO: duipai
GG, no time to write duipai
CCF bless...

*/
